package com.lqzs.businessmanager.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.lqzs.businessmanager.R;
import com.lqzs.businessmanager.bean.ReplyInfo;
import com.lqzs.businessmanager.ui.main.activity.DetailCommentActivity;
import com.lqzs.businessmanager.ui.main.activity.EditCommentActivity;

import java.util.ArrayList;
import java.util.List;

public class DetailCommentAdapter extends BaseAdapter {

    private final Context mContext;
    private List<ReplyInfo> mItems = new ArrayList<>();


    public DetailCommentAdapter(Context context) {
        this.mContext = context;
    }

    public void setItems(List<ReplyInfo> accessInfos){
        if (accessInfos != null && !accessInfos.isEmpty()) {
            mItems.clear();
            mItems.addAll(accessInfos);
            notifyDataSetChanged();
        }
    }

    public List<ReplyInfo> getItems() {
        // TODO: 2025/2/8 只返回变动的
        return mItems;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public ReplyInfo getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO: 2025/2/4  利用 viewholder 复用 view
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_detail_comment, parent, false);
        }
        ReplyInfo info = mItems.get(position);



        convertView.findViewById(R.id.to_edit_reply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, EditCommentActivity.class);
                intent.putExtra("reply_item",mItems.get(position));
                intent.putExtra("position",position);
                intent.putExtra("title",((DetailCommentActivity) mContext).platformName);
                intent.putExtra("address",((DetailCommentActivity) mContext).address);
                ((DetailCommentActivity) mContext).startActivityForResult(intent,6);
            }
        });


        TextView commentView = convertView.findViewById(R.id.tv_detail_comment);
        commentView.setText(String.format("%s : %s", info.getKey(), info.getReviewerInfos()));

        TextView detail_item_time=convertView.findViewById(R.id.detail_item_time);
        TextView detail_item_user_name =  convertView.findViewById(R.id.detail_item_user_name);
        TextView detail_item_user_name_user_comment =  convertView.findViewById(R.id.detail_item_user_name_user_comment);
        TextView detail_item_user_name_ai_comment =  convertView.findViewById(R.id.detail_item_user_name_ai_comment);

        detail_item_time.setText(info.getReviewDate()==null?"":info.getReviewDate());
        detail_item_user_name.setText(info.getReviewer()==null?"":info.getReviewer());
        detail_item_user_name_user_comment.setText(info.getReviewerInfos()==null?"":info.getReviewerInfos());
        detail_item_user_name_ai_comment.setText(info.getReplyContent()==null?"":info.getReplyContent());

        EditText replayView = convertView.findViewById(R.id.tv_detail_replay);
        replayView.setText(info.getReplyContent());
        replayView.setOnFocusChangeListener((view, hasFocus) -> {
            if (!hasFocus) {
                String trim = replayView.getText().toString().trim();
                info.setReplyContent(trim);
            }
        });
        return convertView;
    }
}
